package com.wanlian.dao.impl;

import com.wanlian.common.ConstantCode;
import com.wanlian.dao.StorageDao;
import com.wanlian.dao.StorageItemDao;
import com.wanlian.entity.Question;
import com.wanlian.entity.Storage;
import com.wanlian.entity.StorageItem;
import com.wanlian.model.ResultModel;
import com.wanlian.util.StringUtil;
import com.wanlian.vo.Page;
import com.wanlian.vo.PhotoItemVo;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class StorageItemDaoImpl extends GenericDaoImpl<StorageItem> implements StorageItemDao {

    public StorageItemDaoImpl() {
        super(StorageItem.class);
    }


    /**
     * @param storageId
     * @param type
     * @return
     */
    public long queryTotalByStorageIdAndType(int storageId, int type) {
        String sumSql = String.format("select sum(fileSize) from %s  where storageId=? and fileType=?", entityClassName());
        Long sum = this.queryCount(sumSql, new Object[]{storageId, type});
        if (sum != null) {
            return sum.longValue();
        } else {
            return 0;
        }
    }


    /**
     * 通过storageId获取用户云盘子项
     *
     * @param storageId
     * @param page
     * @return
     */
    @Override
    public ResultModel queryByStorageId(int storageId, Page page) {
        String hql = String.format("from %s where storageId=?", entityClassName());
        List<StorageItem> lists = this.queryForListByPage(hql, new Object[]{storageId}, page);
        String countSql = String.format("select count(*) from %s  where storageId=?", entityClassName());
        long count = this.queryCount(countSql, new Object[]{storageId});
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }

    /**
     * 通过storageId和文件类型获取用户云盘子项
     *
     * @param storageId
     * @param fileType
     * @return
     */
    @Override
    public ResultModel queryByStorageIdAndFileType(int storageId, int fileType) {
        if (fileType == ConstantCode.STORAGE_FILE_PHOTO) {
            return this.queryPhotoByStorageId(storageId, fileType);
        }
        String hql = String.format("from %s where storageId=? and fileType=?  order by uploadtime desc ", entityClassName());
        List<StorageItem> lists = this.queryForList(hql, new Object[]{storageId, fileType});
        String countSql = String.format("select count(*) from %s where storageId=? and fileType=?", entityClassName());
        long count = this.queryCount(countSql, new Object[]{storageId, fileType});
        ResultModel resultModel = new ResultModel(true, lists, count);
        return resultModel;
    }

    /**
     * 查询图片：结构不一致
     *
     * @param storageId
     * @return
     */
    public ResultModel queryPhotoByStorageId(int storageId, int fileType) {
        String sql = "select xuploadTime from (select DATE_FORMAT(uploadTime,'%Y-%m-%d') as xuploadTime from tb_storageitem where storageId=? and filetype=?  ) as s GROUP BY xuploadTime desc";
        List<Object> times = this.queryForListObjectByPage(sql, new Object[]{storageId, fileType}, null);
        int count = this.queryRowNumByParam(sql, new Object[]{storageId, fileType});
        List<PhotoItemVo> photoItemVos = new ArrayList<>();
        String[] columns = {"id", "fileName", "uploadTime", "filePath", "url", "fileSize"};
        for (Object item : times) {
            String time = item.toString();
            String hql = "select id, fileName,uploadTime,filePath,thumbnail as url,fileSize  from tb_storageitem where storageId=?  and filetype=? and DATE_FORMAT(uploadTime,'%Y-%m-%d')= ? order by uploadtime asc ";
            Object lists = this.querySQLForList(hql, new Object[]{storageId, fileType, time}, columns);
            PhotoItemVo itemVo = new PhotoItemVo();
            itemVo.setTime(time);
            itemVo.setItems(lists);
            photoItemVos.add(itemVo);
        }
        ResultModel resultModel = new ResultModel(true, photoItemVos, count);
        return resultModel;
    }

    public void doDelete(StorageItem storageItem) {
        super.delete(storageItem);
    }

    @Override
    public List<StorageItem> queryUsersBySid(int storageId) {
        List<StorageItem> lists = new ArrayList<StorageItem>();
        String hql = String.format("from %s where storageid = ? ", entityClassName());
        lists = this.queryForList(hql, new Object[]{storageId});
        return lists;
    }

}
